package edu.iss.demo.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.iss.demo.model.Student;
import edu.iss.demo.model.UserDetail;
import edu.iss.demo.service.UserDetailService;

/**
 * @author Sachin
 *
 */
@Controller
@RequestMapping(value = "/userdetail")
public class LoginController {

	@Autowired
	private UserDetailService userDetailService;
	
	private UserDetail userDetails;

	// /login

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView loginPage() {
		ModelAndView mav = new ModelAndView("Login", "userDetail",
				new UserDetail());
		
		return mav;
	}

	private boolean ValidUser(String UN, String PW) {
		boolean result = true;
		List<UserDetail> userList = userDetailService.findAll();
		for (UserDetail user : userList) {
			if (user.getEmployeeId().getEmail().equals(UN)) {
				if (user.getPassword().equals(PW)) {
					result = true;
					userDetails = user;
					return result; // break is also a good option
				} else
					result = false;
			} else
				result = false;
		}
		return result;
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView loginPage(@ModelAttribute @Valid UserDetail userDetail,HttpServletRequest req,
			BindingResult result, final RedirectAttributes redirectAttributes) {
		//System.out.println("loginPage");
		if (ValidUser(userDetail.getEmployeeId().getEmail(),
				userDetail.getPassword())) {
			
			req.getSession().setAttribute("userDetails", userDetails);
			req.getSession().setAttribute("userRole", userDetails.getEmployeeId().getRole());
			ModelAndView mav = new ModelAndView("redirect:/index.html");
			
			
			// change the home screen options 
			if(userDetails.getEmployeeId().getRole() != null && userDetails.getEmployeeId().getRole().equals("Manager")){
				// for manager
				mav = new ModelAndView("AdminHome");
				//mav = changeNavPattern(1);
			}else if (userDetails.getEmployeeId().getRole() != null &&userDetails.getEmployeeId().getRole().equals("Employee")) {
				// for Employee
				mav = new ModelAndView("AdminHome");
				///mav = changeNavPattern(2);
			}else if (userDetails.getEmployeeId().getRole() == null || userDetails.getEmployeeId().getRole().trim().length() <1 ) {
				// for Admin
				mav = new ModelAndView("AdminHome");
				//mav = changeNavPattern(3);
			}
			

			//ModelAndView mav = new ModelAndView("redirect:/index.html");
			
			String message = "Hi " + userDetails.getEmployeeId().getName();
			
			req.getSession().setAttribute("userMessage", message);
			
			
			
			return mav;
		} else {

			String message = "Incorrect Username and Password";
			redirectAttributes.addFlashAttribute("message", message);
			return new ModelAndView("Login");
		}
	}
	
	
	
	
	

}
